const AUTHENTICATED_KEY = "IS_AUTHENTICATED";
const ORDER_FOLLOW = "ORDER_FOLLOW";
const PRINCIPAL_KEY = "PRINCIPAL";
const CURRENT_ORGANIZATION_KEY = "currentOrganization";
const ORGANIZATION_TREE_KEY = "organizationTree";

function getIsAuthenticated() {
    return JSON.parse(sessionStorage.getItem(AUTHENTICATED_KEY)) || false;
}

export function getOpenFollow() {
    return sessionStorage.getItem(ORDER_FOLLOW) || false;
}

export function setOpenFollow(value) {
    sessionStorage.setItem(
        ORDER_FOLLOW,
        value
    );
}

export function getPrincipal() {
    return JSON.parse(sessionStorage.getItem(PRINCIPAL_KEY)) || {};
}

function createIdentity(principal) {
    const {
        id,
        account,
        fullName,
        organizationId,
        token,
        organizationTree,
        admin,
        permissions,
        canCheckData,
        notice
    } = principal;
    const isAuthenticated = getIsAuthenticated();
    return {
        isAuthenticated,
        id,
        account,
        fullName,
        organizationId,
        token,
        admin,
        organizationTree,
        permissions,
        canCheckData,
        notice
    };
}

export const identity = createIdentity(getPrincipal());
export default identity;

export function login({
                          id,
                          account,
                          fullName,
                          organizationId,
                          token,
                          organizationTree = [],
                          admin,
                          permissions,
                          canCheckData,
                          notice
                      }) {
    let newPer;
    if (permissions && permissions.indexOf("首页") < 0) {
        newPer = permissions + ",首页";
    } else {
        newPer = permissions;
    }
    if (newPer === undefined || newPer === null) {
        newPer = "";
    }
    if (newPer.indexOf("个人中心") < 0) {
        newPer = "个人中心," + newPer;
    }
    sessionStorage.setItem(AUTHENTICATED_KEY, JSON.stringify(true));
    sessionStorage.setItem(
        PRINCIPAL_KEY,
        JSON.stringify({id, account, fullName, organizationId, token, admin, permissions: newPer, canCheckData, notice})
    );
    if (organizationId === undefined && organizationTree && organizationTree[0].id) {
        organizationId = organizationTree[0].id;
    }
    sessionStorage.setItem(
        CURRENT_ORGANIZATION_KEY,
        organizationId
    );
    sessionStorage.setItem(
        ORGANIZATION_TREE_KEY,
        JSON.stringify(organizationTree)
    );
    Object.assign(identity, {
        id,
        account,
        fullName,
        organizationId,
        token,
        admin,
        permissions: newPer,
        canCheckData,
        notice,
        isAuthenticated: true
    });
}

export function getOrganizationTree() {
    return JSON.parse(sessionStorage.getItem(ORGANIZATION_TREE_KEY));
}

// 获取上一次的查询条件
export function getLastFilter(KEY) {
    return JSON.parse(sessionStorage.getItem(KEY)) || null;
}

export function saveLastFilter(KEY, jsonObj) {
    sessionStorage.setItem(
        KEY,
        JSON.stringify(jsonObj)
    );
}

export function saveColumnSet(KEY, jsonObj) {
    sessionStorage.setItem(
        "ColumnConfig" + KEY,
        JSON.stringify(jsonObj)
    );
}

// 获取上一次的查询条件
export function getColumnSet(KEY) {
    return JSON.parse(sessionStorage.getItem("ColumnConfig" + KEY)) || [];
}

export function getCurrentOrganization() {
    return sessionStorage.getItem(CURRENT_ORGANIZATION_KEY);
}

export function changeOrganization(value) {
    sessionStorage.setItem(
        CURRENT_ORGANIZATION_KEY,
        value
    );
}

export function logout() {
    sessionStorage.removeItem(AUTHENTICATED_KEY);
    sessionStorage.removeItem(PRINCIPAL_KEY);
    sessionStorage.removeItem(CURRENT_ORGANIZATION_KEY);
    sessionStorage.removeItem(ORGANIZATION_TREE_KEY);
    Object.assign(identity, {
        isAuthenticated: false,
        id: null,
        account: null,
        fullName: null,
        organizationId: null,
        token: null,
        admin: false,
        organizationTree: null,
        permissions: [],
        canCheckData: [],
        notice: false
    });
}
